import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:iconify_flutter/iconify_flutter.dart';
import 'package:colorful_iconify_flutter/icons/twemoji.dart';
import 'package:xim/common/style/colors.dart';
import 'package:xim/common/util/device.dart';
import 'package:xim/common/util/getx.dart';

class ChatBottomFacePanelController {

  /// 底部面板高度
  final _height = 0.0.obs;

  double get height => _height.value;

  double setHeight(double height) => _height.value = height;
}

/// 聊天框：底部操作面板
class ChatBottomFacePanel extends StatelessWidget {

  Color? bgColor;

  /// 所有的表情
  final emotions = [
    Emoji(emoji: Twemoji.angry_face, code: "angryFace"),
    Emoji(emoji: Twemoji.anguished_face, code: "anguishedFace"),
    Emoji(emoji: Twemoji.anxious_face_with_sweat, code: "anxious_face_with_sweat"),
    Emoji(emoji: Twemoji.astonished_face, code: "astonished_face"),
    Emoji(emoji: Twemoji.beaming_face_with_smiling_eyes, code: "beaming_face_with_smiling_eyes"),
    Emoji(emoji: Twemoji.broken_heart, code: "broken_heart"),
    Emoji(emoji: Twemoji.confused_face, code: "confused_face"),
    Emoji(emoji: Twemoji.confounded_face, code: "confounded_face"),
    Emoji(emoji: Twemoji.cowboy_hat_face, code: "cowboy_hat_face"),
    Emoji(emoji: Twemoji.downcast_face_with_sweat, code: "downcast_face_with_sweat"),
    Emoji(emoji: Twemoji.drooling_face, code: "drooling_face"),
    Emoji(emoji: Twemoji.exploding_head, code: "exploding_head"),
    Emoji(emoji: Twemoji.expressionless_face, code: "expressionless_face"),
    Emoji(emoji: Twemoji.face_in_clouds, code: "face_in_clouds"),
    Emoji(emoji: Twemoji.face_blowing_a_kiss, code: "face_blowing_a_kiss"),
    Emoji(emoji: Twemoji.face_holding_back_tears, code: "face_holding_back_tears"),
    Emoji(emoji: Twemoji.face_savoring_food, code: "face_savoring_food"),
    Emoji(emoji: Twemoji.face_screaming_in_fear, code: "face_screaming_in_fear"),
    Emoji(emoji: Twemoji.face_vomiting, code: "face_vomiting"),
    Emoji(emoji: Twemoji.face_with_crossed_out_eyes, code: "face_with_crossed_out_eyes"),
    Emoji(emoji: Twemoji.face_with_hand_over_mouth, code: "face_with_hand_over_mouth"),
    Emoji(emoji: Twemoji.face_with_diagonal_mouth, code: "face_with_diagonal_mouth"),
    Emoji(emoji: Twemoji.face_with_head_bandage, code: "face_with_head_bandage"),
    Emoji(emoji: Twemoji.face_with_monocle, code: "face_with_monocle"),
    Emoji(emoji: Twemoji.face_with_medical_mask, code: "face_with_medical_mask"),
    Emoji(emoji: Twemoji.face_with_raised_eyebrow, code: "face_with_raised_eyebrow"),
    Emoji(emoji: Twemoji.face_with_tears_of_joy, code: "face_with_tears_of_joy"),
    Emoji(emoji: Twemoji.frowning_face_with_open_mouth, code: "frowning_face_with_open_mouth"),
    Emoji(emoji: Twemoji.face_with_rolling_eyes, code: "face_with_rolling_eyes"),
    Emoji(emoji: Twemoji.face_with_spiral_eyes, code: "face_with_spiral_eyes"),
    Emoji(emoji: Twemoji.face_with_symbols_on_mouth, code: "face_with_symbols_on_mouth"),
    Emoji(emoji: Twemoji.face_with_thermometer, code: "face_with_thermometer"),
    Emoji(emoji: Twemoji.neutral_face, code: "neutral_face"),
    Emoji(emoji: Twemoji.growing_heart, code: "growing_heart"),
    Emoji(emoji: Twemoji.rolling_on_the_floor_laughing, code: "rolling_on_the_floor_laughing"),
    Emoji(emoji: Twemoji.shushing_face, code: "shushing_face"),
    Emoji(emoji: Twemoji.sleeping_face, code: "sleeping_face"),
    Emoji(emoji: Twemoji.smiling_face, code: "smiling_face"),
    Emoji(emoji: Twemoji.smiling_face_with_heart_eyes, code: "smiling_face_with_heart_eyes"),
    Emoji(emoji: Twemoji.smiling_face_with_open_hands, code: "smiling_face_with_open_hands"),
    Emoji(emoji: Twemoji.smirking_face, code: "smirking_face"),
    Emoji(emoji: Twemoji.smirking_face, code: "smirking_face"),
    Emoji(emoji: Twemoji.pleading_face, code: "pleading_face"),
    Emoji(emoji: Twemoji.smiling_face_with_halo, code: "smiling_face_with_halo"),
    Emoji(emoji: Twemoji.smiling_face_with_hearts, code: "smiling_face_with_hearts"),
    Emoji(emoji: Twemoji.smiling_face_with_smiling_eyes, code: "smiling_face_with_smiling_eyes"),
    Emoji(emoji: Twemoji.smiling_face_with_sunglasses, code: "smiling_face_with_sunglasses"),
    Emoji(emoji: Twemoji.pouting_face, code: "pouting_face"),
    Emoji(emoji: Twemoji.hushed_face, code: "hushed_face"),
    Emoji(emoji: Twemoji.kissing_face_with_closed_eyes, code: "kissing_face_with_closed_eyes"),
    Emoji(emoji: Twemoji.sneezing_face, code: "sneezing_face"),
    Emoji(emoji: Twemoji.sneezing_face, code: "sneezing_face"),
    Emoji(emoji: Twemoji.squinting_face_with_tongue, code: "squinting_face_with_tongue"),
    Emoji(emoji: Twemoji.upside_down_face, code: "upside_down_face"),
    Emoji(emoji: Twemoji.upside_down_face, code: "upside_down_face"),
    Emoji(emoji: Twemoji.fearful_face, code: "fearful_face"),
    Emoji(emoji: Twemoji.money_mouth_face, code: "money_mouth_face"),
    Emoji(emoji: Twemoji.pile_of_poo, code: "pile_of_poo"),
    Emoji(emoji: Twemoji.thinking_face, code: "thinking_face"),
    Emoji(emoji: Twemoji.winking_face_with_tongue, code: "winking_face_with_tongue"),
    Emoji(emoji: Twemoji.yawning_face, code: "yawning_face"),
    Emoji(emoji: Twemoji.yawning_face, code: "yawning_face"),
    Emoji(emoji: Twemoji.zany_face, code: "zany_face"),
    Emoji(emoji: Twemoji.weary_cat, code: "weary_cat"),
    Emoji(emoji: Twemoji.weary_cat, code: "weary_cat"),
    Emoji(emoji: Twemoji.zipper_mouth_face, code: "zipper_mouth_face"),
    Emoji(emoji: Twemoji.zzz, code: "zzz"),
    Emoji(emoji: Twemoji.angry_face_with_horns, code: "angry_face_with_horns"),
    Emoji(emoji: Twemoji.alien, code: "alien"),
    Emoji(emoji: Twemoji.anger_symbol, code: "anger_symbol"),
    Emoji(emoji: Twemoji.beating_heart, code: "beating_heart"),
    Emoji(emoji: Twemoji.bomb, code: "bomb"),
    Emoji(emoji: Twemoji.dashing_away, code: "dashing_away"),
    Emoji(emoji: Twemoji.hundred_points, code: "hundred_points"),
  ];

  /// 常用的表情
  var frequentEmotions = [
    Twemoji.cowboy_hat_face,
    Twemoji.cold_face,
    Twemoji.crying_face,
    Twemoji.disappointed_face,
    Twemoji.downcast_face_with_sweat,
    Twemoji.drooling_face,
    Twemoji.exploding_head,
    Twemoji.expressionless_face,
    Twemoji.face_in_clouds,
    Twemoji.face_blowing_a_kiss,
  ];

  var controller = Get.findOrInject(builder: () => ChatBottomFacePanelController());

  ChatBottomFacePanel({Key? key, required double height, this.bgColor}) : super(key: key) {
    controller.setHeight(height);
  }

  @override
  Widget build(BuildContext context) {
    var bgColor = ModelColor(
        darkColor: Colors.black,
        lightColor: const Color(0xffededed),
        model: PlatformUtils.getBrightness(context)
    );
    return Obx(() {
      return Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        height: controller.height,
        color: bgColor.color,
        child: Stack(
          children: [
            SingleChildScrollView(
              physics: const BouncingScrollPhysics(),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  if(frequentEmotions.isNotEmpty) ...[
                    Text("最近常用", style: TextStyle(fontSize: 12, color: bgColor.reverse),),
                    const SizedBox(height: 12,),
                    GridView.count(
                      crossAxisCount: 8,
                      shrinkWrap: true,
                      mainAxisSpacing: 18,
                      crossAxisSpacing: 18,
                      children: List.generate(min(frequentEmotions.length, 8), (index) => Iconify(frequentEmotions[index],)),
                    ),
                    const SizedBox(height: 12,),
                  ],
                  Text("所有表情", style: TextStyle(fontSize: 12, color: bgColor.reverse)),
                  const SizedBox(height: 12,),
                  GridView.count(
                    physics: const NeverScrollableScrollPhysics(),
                    crossAxisCount: 8,
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    mainAxisSpacing: 18,
                    crossAxisSpacing: 18,
                    children: List.generate(emotions.length, (index) => InkWell(
                      onTap: (){

                      },
                      child: Iconify(emotions[index].emoji,),
                    )),
                  )
                ],
              ),
            ),
            /// 删除控件
            const Positioned(
                right: 2,
                bottom: 40,
                child: DecoratedBox(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(6))
                    ),
                  child: Padding(
                    padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16),
                    child: Center(child: Icon(Icons.close, size: 14,)),
                  ),
                )
            )
          ],
        ),
      );
    });
  }
}

class Emoji {

  /// 编码
  final String code;

  /// svg
  final String emoji;

  Emoji({required this.code, required this.emoji});

}
